package com.bh.ly.oa.adapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bh.ly.oa.R;

public class OrderDetailContentViewAdapter extends BaseAdapter {

	Context context = null;
	JSONArray array = null;

	public OrderDetailContentViewAdapter(Context contex, String data) {
		this.context = contex;
		this.array = this.getJsonArryData(data);
	}

	@Override
	public int getCount() {
		return array.length();
	}

	@Override
	public Object getItem(int position) {
		return array.opt(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		OrderItem orderItem = null;
		if (convertView == null) {
			orderItem = new OrderItem();
			convertView = LayoutInflater.from(context).inflate(R.layout.orderdetail_item, null);
		} else {
			orderItem = (OrderItem) convertView.getTag();
		}

		JSONObject jsonObject = (JSONObject) array.opt(position);
		initView(position, convertView, orderItem);
		try {
			orderItem.label.setText(jsonObject.getString("key") + ":");
			orderItem.labelvalue.setText(jsonObject.getString("value"));
			if (OrderDetailAdapter.isDigst(jsonObject.getString("value")))
				orderItem.labelvalue.setGravity(Gravity.RIGHT);
			else
				orderItem.labelvalue.setGravity(Gravity.LEFT);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return convertView;
	}

	private void initView(int position, View convertView, OrderItem orderItem) {

		orderItem.label = (TextView) convertView.findViewById(R.id.label);
		orderItem.labelvalue = (TextView) convertView.findViewById(R.id.value);
		convertView.setTag(orderItem);
	}

	class OrderItem {
		TextView label;
		TextView labelvalue;
	}

	private JSONArray getJsonArryData(String data) {
		JSONArray jsonarray = new JSONArray();
		JSONObject ad = null;
		try {
			JSONObject jb = new JSONObject(data);
			ad = new JSONObject();
			ad.put("key", "订  单  号");
			ad.put("value", jb.get("orderno"));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "供应商编码");
			ad.put("value", jb.get("suppliercode"));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "供货商名称");
			String suppliername = jb.get("suppliername").toString();
			ad.put("value", suppliername);
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "订货部门");
			ad.put("value", jb.get("deptname"));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "商品编码");
			ad.put("value", jb.get("gdsincode"));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "商品名称");
			ad.put("value", getStringtext(jb.get("gdsname")));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "条  形  码");
			ad.put("value", getStringtext(jb.get("stripecode")));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "基本销售单位");
			ad.put("value", getStringtext(jb.get("baseunit")));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "订货数量");
			ad.put("value", getStringtext(jb.get("amount")));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "订货价格");
			ad.put("value", getStringtext(jb.get("inprice")));
			jsonarray.put(ad);

			ad = new JSONObject();
			ad.put("key", "订货金额");
			ad.put("value", getDatetext(jb.get("inmoney")));
			jsonarray.put(ad);

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return jsonarray;
	}

	public String getDatetext(Object ob) {
		if (ob.toString().equals("null")) {
			return "";
		} else {
			return ob.toString().substring(0, ob.toString().length() - 2);
		}
	}

	public String getStringtext(Object ob) {
		if (ob.toString().equals("null")) {
			return "";
		} else {
			return ob.toString();
		}
	}
}
